﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyCommunity.BusinessLogic;
using MyCommunity.Models;

namespace MyCommunity.Controllers
{
    public class EventController : Controller
    {
        private readonly IEventService _eventService;
        private readonly IUserEventAttendanceService _userEventAttendanceService;
        private readonly IUserService _userService;

        public EventController()
        {
            _eventService = new EventService(new EventDAL());
            _userEventAttendanceService = new UserEventAttendanceService(new UserEventAttendanceDAL());
            _userService = new UserService(new UserDAL());
        }

        // For Admin
        // GET: /Event/
        [Authorize]
        public ActionResult Index(String searchString)
        {
            String userMail = User.Identity.Name;
            User user = _userService.getUser(userMail);
            

            var eventListView = new EventListViewModel<EventViewModel>()
            {
                events = _eventService.GetAllEvents(searchString)
            };

            eventListView.SetRole(user.IsAdmin);

            return View(eventListView);
        }


        //
        // GET: /Event/Details/5
        [Authorize]
        public ActionResult Details(int id)
        {
            EventViewModel eventT = new EventViewModel(_eventService.GetEvent(id));
            List<UserEventAttendance> list = _userEventAttendanceService.GetAllUsersForEvent(id);
            eventT.SetAttendance(list);

            String userMail = User.Identity.Name;
            User user = _userService.getUser(userMail);
            eventT.SetRole(user.IsAdmin);

            return View(eventT);
        }

        //
        // GET: /Event/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Event/Create
        [Authorize]
        [HttpPost]
        public ActionResult Create(EventRegisterModel eventRegisterModel)
        {
            if (ModelState.IsValid)
            {

                _eventService.CreateEvent(eventRegisterModel);
                _eventService.SaveChanges();

                return RedirectToAction("Index", "Event");
            }
            return View(eventRegisterModel);
        }
        
        //
        // GET: /Event/Edit/5
        [Authorize]
        public ActionResult Edit(int id)
        {
            Event eventOld = _eventService.GetEvent(id);

            return View(new EventViewModel(eventOld));
        }

        //
        // POST: /Event/Edit/5
        [Authorize]
        [HttpPost]
        public ActionResult Edit(EventViewModel model)
        {
            Event eventOld = _eventService.GetEvent(model.Id);

            try
            {
                model.ApplyChanges(eventOld);
                _eventService.SaveChanges();

                return RedirectToAction("Details", "Event", new { id = model.Id });
            }
            catch
            {
                return View(new EventViewModel(eventOld));
            }
        }

        //
        // GET: /Event/Delete/5
        [Authorize]
        public ActionResult Delete(int id)
        {
            Event eventOld = _eventService.GetEvent(id);

            return View(new EventViewModel(eventOld));
        }

        //
        // POST: /Event/Delete/5
        [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            Event eventDel = _eventService.GetEvent(id);
            _eventService.DeleteEvent(eventDel);
            _eventService.SaveChanges();

            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //[HttpPost]
        [Authorize]
        public ActionResult EventOptIn(int id, string data)
        {
            String userMail = User.Identity.Name;
            User user = _userService.getUser(userMail);

            _userEventAttendanceService.OptInEvent(user, id, data);
            _userEventAttendanceService.SaveChanges();

            //return View(new EventViewModel(_eventService.GetEvent(id)));
            return RedirectToAction("Details", new { id });
        }

    }
}
